<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-31
 * Time: 下午2:44
 */
namespace V1;
class E_Product extends \ResourceClass {
    public $isActive = true;
    public $remark = '获取产品详情';

    public function get(array $req, array $post, &$result)
    {
        $result = array();
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['id'])) {
            return 'require product id';
        }
        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$req['enterpriseId'],$req['id'].$req['t'],$req['k']) == false) {
            return 'data illegal';
        }
        if (!isset($req['id']) || $req['id'] == false) {
            return 'require id';
        }

        try {
            $product_cls = \Biz\ProductHandler::getProductById($req['id']);
            \Biz\ProductHandler::completeChannelInfoByProductCls($req['enterpriseId'],$product_cls);
            $items =  self::product($product_cls);
            $result = $items;
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return '';
    }

    //拼接$product_list_cls数据
    public static function product(\Cls\Product\Product $product_list_cls)
    {
        $product_info = array();
        $product_info['id'] = $product_list_cls->getId();
        $product_info['enterprise_id'] = $product_list_cls->getEnterpriseId();
        $product_info['brand_id'] = $product_list_cls->getBrandId();
        $product_info['name'] = $product_list_cls->getName();
        $product_info['icon_url'] = $product_list_cls->getIconUrl();
        $product_info['repertory_warning'] = $product_list_cls->getStockWarning();
        $product_info['individual_packing'] = $product_list_cls->getSellUnitString(); // 老版本
        $product_info['mass_packing'] = $product_list_cls->getSellUnitString(); // 老版本
        $product_info['sell_unit_string'] = $product_list_cls->getSellUnitString();
        $product_info['remark'] = $product_list_cls->getDescription();
        $product_info['insert_time'] = $product_list_cls->getCreateTime();
        $product_info['is_del'] = $product_list_cls->getDelStatus();
        $product_info['brandname'] = $product_list_cls->getBrandInfo()->getName();
        $product_info['scroller_imgs'] = array($product_list_cls->getIconUrl());
        $product_info['detail_imgs'] = array();
        $product_info['collect_stock'] = 0;
        foreach ($product_list_cls->getSkuList() as $sku) {
            $product_info['collect_stock'] += $sku->getStock(); //累加库存信息
            if (trim($sku->getAttr()) == 'a:0:{}') {
                $sku->setAttr('【统一属性】');
            } else {
                $tmp_attr = unserialize(trim($sku->getAttr()));
                $tmp_attr_str = '';
                if (is_array($tmp_attr)) {
                    foreach ($tmp_attr as $name=>$attr) {
                        $tmp_attr_str .= $name.':'.$attr.',';
                    }
                }
                $sku->setAttr(substr($tmp_attr_str,0,-1));
            }
            $tmp_sku =  array(
                'id' =>$sku->getId(),
                'product_id' =>$sku->getProductId(),
                'imgurl' =>$sku->getImageUrl(),
                'attr' =>$sku->getAttr(),
                'code' =>$sku->getCode(),
                'sellprice' =>$sku->getSellingPrice(),
                'cost' =>$sku->getCost(),
                'stock' =>$sku->getStock(),
                'insert_time' =>$sku->getCreateTime(),
                'update_time' =>$sku->getUpdateTime()
            );
            $product_info['sku_info'][] = $tmp_sku;
        }
        foreach($product_list_cls->getChannelProduct() as $channel_cls){
            $discount = $channel_cls->getChannelInfo()->getDiscount();
            $channelname = $channel_cls->getChannelInfo()->getName();
            if($discount == null){
                $channel_discount = '';
            }else{
                $channel_discount = $discount;
            }
            if($channelname == null){
                $channel_name = '';
            }else{
                $channel_name = $channelname;
            }
            $tmp = array(
                'id' =>$channel_cls->getId(),
                'product_id' =>$channel_cls->getProductId(),
                'channel_id' =>$channel_cls->getChannelId(),
                'price' =>$channel_cls->getPriceRange(),
                'issale' =>$channel_cls->getSaleStatus(),
                'channel_name' =>$channel_name,
                'channel_discount' =>$channel_discount
            );
            $product_info['channel_info'][] =$tmp;
        }

        return $product_info;
    }

    public function put(array $req, array $post, &$result)
    {
        if (!array_key_exists('id', $req) && !empty($req['id'])) {
            return 'require resource id';
        }
        if (!isset($req['uid'])) {
            return 'require user id ';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }

        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'],$req['enterpriseId'], $req['id'] . $req['t'] . $post['payload'], $req['k']) == false) {
            return 'data illegal';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data,$data);

        $product_handler = new \Biz\ProductHandler();
        $product_cls = $product_handler->getProductById($req['id']);
        if (isset($data['brandId'])) {
            $product_cls->setId($req['brandId']);
        }
        if (isset($data['name'])) {
            $product_cls->setName($req['name']);
        }
        try {
            $product_handler ->updateProductNameAndBrandId($product_cls);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        return '';
    }
}
